home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 06 - 1990 / 06.11 Nov 90 / FKEY Source Code / Selection Sort / GetSelection.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-11-20  |  2.2 KB  |  120 lines  |  [TEXT/KAHL]

  1. #include "ToolSort.h"
  2.  
  3. /*=========================================================================
  4. Module:        GetValidSel
  5.  
  6. Purpose:    Get a Selection and verify if Selection is correct type, 
  7.             correct format.
  8.             
  9. Returns:    Valid-Selection            
  10.  
  11. Functional Details:    
  12.  
  13.             Verify if Selection Type IS 'TEXT'
  14.             Check if last character of Selection is eol
  15.             Report errors to user
  16.  
  17. =========================================================================*/
  18.  
  19. GetValidSel(validSel)
  20.     SelRec        *validSel;        /* Ptr to SelRec */
  21.     {
  22.     Handle        scrap;            /* Handle to scrap */
  23.     Handle        selection;        /* Handle to scrap */
  24.     int         error = 0;
  25.     long        length;            /* Scrap length */
  26.     
  27.     length = GetSel( &scrap );
  28.     if ( length > 0 )
  29.         {
  30.         if ( (*scrap)[length-1] != '\r' )
  31.             error += End_Error;
  32.         else
  33.             {
  34.             HLock( scrap );
  35.             error = PtrToHand(*scrap, &selection, length); /* copy scrap */
  36.             HUnlock( scrap );
  37.             
  38.             if (error != noErr)
  39.                 error = Memory_Error;
  40.             }
  41.         }
  42.     else
  43.         error = -length;        /* if length <0 then error */
  44.     
  45.     if ( error != No_Error )
  46.         {
  47.         validSel->sel = 0L;        /* On error set handle to Nil */
  48.         validSel->length = 0;
  49.         }
  50.     else
  51.         {
  52.         validSel->sel = selection;
  53.         validSel->length = length;
  54.         }
  55.         
  56.     return( error );
  57.     }
  58.     
  59. long GetSel( sel )
  60.     Handle *sel;                /* Ptr to a Handle to the scrap */
  61.     {
  62.     int err = noErr;
  63.     long len;                    /* len of scrap */
  64.     
  65. #if DESK_SCRAP                    /* Desk Scrap */
  66. {
  67.     long     offset;                /* offset to start of data */
  68.     ResType type = 'TEXT';
  69.  
  70.     *sel = NewHandle(0L);        /* Allocate minimum space*/
  71.     if (*sel == NIL)            /* error check */
  72.         err += Memory_Error;
  73.     else
  74.         {
  75.         len = GetScrap( *sel, type, &offset);
  76.         
  77.         if ( len <= 0 )
  78.             {
  79.             switch( len )
  80.                 {
  81.                 case NULL:
  82.                     err += Empty_Error;
  83.                     break;
  84.                 case noTypeErr:
  85.                     err += Type_Error;
  86.                     break;
  87.                 case memFullErr:
  88.                     err += Memory_Error;
  89.                     break;
  90.                 default:
  91.                     if (len < 0)
  92.                         err += Sys_Error;
  93.                 }
  94.             *sel = 0L;            /* On error set handle to NIL */
  95.             len = -err;            /* if length <0 then error */
  96.             }
  97.         }
  98.         
  99.     return( len );
  100.         
  101. }
  102.  
  103. #else                            /* Text edit scrap */
  104.  
  105.     *sel = TEScrapHandle();
  106.     len = TEGetScrapLen();
  107.     
  108.     if ( len <= 0)
  109.         {
  110.         err = Empty_Error;
  111.         *sel = 0L;                /* On error set handle to NIL */
  112.         len = -err;                /* if length <0 then error */
  113.         }
  114.  
  115.     return( len );
  116.     
  117. #endif
  118.     }
  119.     
  120.